package com.lilicould.controller;

import com.lilicould.entity.RepairRequest;
import com.lilicould.service.RepairService;
import com.lilicould.service.impl.RepairServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/repair/request")
public class RepairRequestController extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RepairService repairService = new RepairServiceImpl();

        // 获取输入参数
        String buildingId = req.getParameter("buildingId");
        String apartmentNo = req.getParameter("apartmentNo");
        String description = req.getParameter("description");

        // 创建RepairRequest对象
        RepairRequest repairRequest = new RepairRequest(null, buildingId, apartmentNo, description, null, null);

        // 调用插入报修请求服务
        boolean result = repairService.insertRepairRequest(repairRequest);

        if (result) {
            req.setAttribute("success", "已提交报修请求");
            req.getRequestDispatcher("/page/tenant/repair.jsp").forward(req, resp);
        } else {
            // 插入失败
            req.getSession().setAttribute("error", "插入失败");
            req.getRequestDispatcher("/page/err.jsp").forward(req, resp);
        }
    }
}
